<?php

namespace App\Dao;

use App\Model\BillModel;
use Illuminate\Support\Facades\DB;

class BillDao extends BaseDao
{
    public static function get_bills_page(array $all)
    {
        $list =  self::get_bill_builder($all)->with('userInfo')->orderByDesc('created_at');

        if(checkIsHasField($all,'search_key')){
            $user_ids = DB::select("SELECT user_id FROM df_users WHERE user_phone LIKE '%".$all['search_key']."%' OR nickname LIKE '%".$all['search_key']."%';");
            if(!empty($user_ids)){
                $user_ids = json_decode(json_encode($user_ids),true);
                $list = $list->whereIn('user_id',array_column($user_ids,'user_id'));
            }else{
                $list = $list->whereIn('user_id',[]);
            }
        }

        return $list->paginate();
    }


    public static function audit(array $params)
    {
        DB::beginTransaction();
        $res = self::get_bill_builder([
            'bill_id'=>$params['bill_id']
        ])->update([
            'status'=>$params['status'],
            'text_str'=>$params['text_str']
        ]);
        if($params['status'] == 2){
            $billInfo= self::get_bill_builder([
                'bill_id'=>$params['bill_id']
            ])->first();
            //增加回去
            $res !== false && $res = UsersDao::get_user_builder([
                'user_id'=>$billInfo['user_id']
            ])->increment('money',$billInfo['num']);
        }
        if($res === false){
            DB::rollBack();
            self::error('审核失败');
        }
        DB::commit();
    }

    public static function get_bill_builder($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = BillModel::query();

        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });


        $query->when(checkIsHasField($params,'pm'),function ($query)use ($params){
            $query->where('pm',$params['pm']);
        });
        $query->when(checkIsHasField($params,'from_user_id'),function ($query)use ($params){
            $query->where('from_user_id',$params['from_user_id']);
        });



        $query->when(checkIsHasField($params,'type'),function ($query)use ($params){
            $query->where('type',$params['type']);
        });


        $query->when(checkIsHasField($params,'status'),function ($query)use ($params){
            $query->where('status',$params['status']);
        });

        $query->when(checkIsHasField($params,'bill_id'),function ($query)use ($params){
            $query->where('bill_id',$params['bill_id']);
        });


        $query->when(checkIsHasField($params,'start_time'),function ($query)use ($params){
            $query->where('created_at','>=',$params['start_time']);
        });

        $query->when(checkIsHasField($params,'end_time'),function ($query)use ($params){
            $query->where('end_time','<=',$params['end_time']);
        });

        return $query;
    }


    public static function get_bills(array $params)
    {
        $list = self::get_bill_builder($params)->orderByDesc('created_at')->get();
        return $list;
    }


    }
